/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.stat.content.log;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.StatDetailLog;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.stat.StatContentConstants;
import han.bifang.gwt.client.stat.StatMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class LogDetailContent extends Content {

	private StatMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;
	private VerticalPanel panel;
	private Button b1, b2, b3, b4, b5;
	private StatDetailLog log;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			Boolean canRetry = (Boolean) res.getResponse();
			b1.setEnabled(true);
			b2.setEnabled(true);
			if (log.getStatus().equals(EntityConstants.STATUS_ERROR)) {
				b3.setEnabled(true);
				b4.setEnabled(true);
			}
			if (canRetry
					&& log.getStatus().equals(EntityConstants.STATUS_ERROR)) {
				if (PrivilegChecker.hasPrivilege(context, null)) {
					b5.setVisible(true);
				}
			}
			// Window.alert(cons.modifyOk());
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public LogDetailContent(StatMessages cons, BifangGWTServiceAsync service,
			GwtContext context, StatDetailLog log) {
		this.service = service;
		this.context = context;
		this.cons = cons;
		this.log = log;

		panel = new VerticalPanel();
		panel.setWidth("100%");
		panel.setSpacing(5);
		initWidget(panel);

	}

	public void onLoad() {
		Grid table = new Grid(8, 2);
		table.setWidth("70%");
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "40%");
		cf.setWidth(1, "60%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");
		ff.setStyleName(5, 0, "tablecol");
		ff.setStyleName(6, 0, "tablecol");
		ff.setStyleName(7, 0, "tablecol");

		table.setHTML(0, 0, cons.logId());
		table.setHTML(1, 0, cons.busCode());
		table.setHTML(2, 0, cons.serviceCode());
		table.setHTML(3, 0, cons.version());
		table.setHTML(4, 0, cons.requestId());
		table.setHTML(5, 0, cons.status());
		table.setHTML(6, 0, cons.time());
		table.setHTML(7, 0, cons.responseTime());

		table.setHTML(0, 1, log.getLogId());
		table.setHTML(1, 1, log.getBusCode());
		table.setHTML(2, 1, log.getServiceCode());
		table.setHTML(3, 1, log.getVersion());
		table.setHTML(4, 1, log.getRequestId());
		table.setHTML(5, 1,
				log.getStatus().equals(EntityConstants.STATUS_OK) ? cons
						.success() : cons.error());
		table.setHTML(6, 1, log.getTime());
		if (log.getStatus().equals(EntityConstants.STATUS_OK))
			table.setHTML(7, 1, String.valueOf(log.getResponseTime() / 1000));
		else
			table.setHTML(7, 1, "N/A");

		panel.add(table);

		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(10);
		b1 = new Button(cons.viewRequest());
		b2 = new Button(cons.viewResponse());
		b3 = new Button(cons.viewError());
		b4 = new Button(cons.viewRetry());
		b5 = new Button(cons.retry());

		b1.setEnabled(false);
		b2.setEnabled(false);
		b3.setEnabled(false);
		b4.setEnabled(false);
		b5.setVisible(false);

		hp.add(b1);
		hp.add(b2);
		hp.add(b3);
		hp.add(b4);
		hp.add(b5);
		panel.add(hp);

		b1.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				DialogBox dialogBox = createRequestDialogBox();
				dialogBox.setText(cons.viewRequest());
				dialogBox.center();
				dialogBox.show();
			}
		});

		b2.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				DialogBox dialogBox = createResponseDialogBox();
				dialogBox.setText(cons.viewResponse());
				dialogBox.center();
				dialogBox.show();
			}
		});

		b3.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				DialogBox dialogBox = createErrorDialogBox();
				dialogBox.setText(cons.viewError());
				dialogBox.center();
				dialogBox.show();
			}
		});

		b4.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				DialogBox dialogBox = createRetryDetailDialogBox();
				dialogBox.setText(cons.viewRetry());
				dialogBox.center();
				dialogBox.show();
			}
		});

		b5.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				DialogBox dialogBox = createRetryDialogBox();
				dialogBox.setText(cons.retry());
				dialogBox.center();
				dialogBox.show();
			}
		});

		panel.add(new HTML("&nbsp;"));
		Button re = new Button(cons.close());
		panel.add(re);

		re.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				listener.onContentChanged(StatContentConstants.LOG_DETAIL,
						StatContentConstants.SEARCH_LOG_PAGE, null);

			}
		});

		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.stat.LogDetailAction");
		ArrayList<String> r = new ArrayList<String>();
		r.add(log.getLogId());
		r.add(log.getServiceCode());
		r.add(log.getVersion());
		req.setRequest(r);

		service.call(req, cb);

	}

	private DialogBox createRequestDialogBox() {
		DialogBox dialogBox = new DialogBox();
		dialogBox.setWidth("60%");
		ViewRequestPanel vp = new ViewRequestPanel(cons, service, context,
				dialogBox);
		// dialogBox, g);
		// gp.setSearcher(this);
		dialogBox.setWidget(vp);

		return dialogBox;
	}

	private DialogBox createResponseDialogBox() {
		DialogBox dialogBox = new DialogBox();
		dialogBox.setWidth("50%");
		ViewResponsePanel vp = new ViewResponsePanel(cons, service, context,
				dialogBox);
		// dialogBox, g);
		// gp.setSearcher(this);
		dialogBox.setWidget(vp);

		return dialogBox;
	}

	private DialogBox createErrorDialogBox() {
		DialogBox dialogBox = new DialogBox();
		dialogBox.setWidth("60%");
		ViewErrorPanel vp = new ViewErrorPanel(cons, service, context,
				dialogBox);
		// dialogBox, g);
		// gp.setSearcher(this);
		dialogBox.setWidget(vp);

		return dialogBox;
	}

	private DialogBox createRetryDetailDialogBox() {
		DialogBox dialogBox = new DialogBox();
		dialogBox.setWidth("60%");
		ViewRetryPanel vp = new ViewRetryPanel(cons, service, context,
				dialogBox);
		// dialogBox, g);
		// gp.setSearcher(this);
		dialogBox.setWidget(vp);

		return dialogBox;
	}

	private DialogBox createRetryDialogBox() {
		DialogBox dialogBox = new DialogBox();
		dialogBox.setWidth("60%");
		RetryPanel vp = new RetryPanel(cons, service, context, dialogBox);
		// dialogBox, g);
		// gp.setSearcher(this);
		dialogBox.setWidget(vp);

		return dialogBox;
	}

}
